package com.gilight.base.filter;

import com.alibaba.fastjson.JSONObject;
import com.gilight.base.base.RepStatusCode;
import com.gilight.base.base.ResultEntity;
import org.apache.shiro.web.filter.authz.RolesAuthorizationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 权限认证接口
 *
 * @author wuzz
 * @date 11:31 2019/9/2
 */
public class MyRolesAuthorizationFilter extends RolesAuthorizationFilter {

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws IOException {
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/json");
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setMsg("当前用户角色权限不足!");
        resultEntity.setStatus(RepStatusCode.NO_AUTH_ERROR.getCode());
        httpServletResponse.getWriter().write(JSONObject.toJSON(resultEntity).toString());
        return false;
    }
}
